/**
 * 
 */
package org.mhz.sit.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * @author Mahan
 *
 * Hibernate util class
 */
public class HibernateUtil
{
    private static final SessionFactory mSessionFactory;
    private static ServiceRegistry mServiceRegistry;

    static 
    {
        try 
        {
            final Configuration configuration = new Configuration();
            configuration.configure();
            mServiceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
            mSessionFactory = configuration.buildSessionFactory(mServiceRegistry);
        } 
        catch (Throwable ex) 
        {
            // Log exception!
            throw new ExceptionInInitializerError(ex);
        }

    }

    /**
     * @return The hibernate session
     * @throws HibernateException
     */
    public static Session getSession() throws HibernateException 
    {
        return mSessionFactory.openSession();
    }
}
